<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

class M_menu extends BZ_Model
{
/**
 * @section biskitZ Front-End Menu
 */

	/**
	 * @function front_generate
	 * @description generate the front-end menu
	 */
	function front_generate
		(
			$prm_dbase = 'db_default'
			, $prm_language_abbr = 'en'
			, $prm_menu_sub_use = FALSE, $prm_parent_id = 0
			, $prm_parent_as_link = TRUE
			, $prm_parent_tag_open = '<b class="category_parent">', $prm_parent_tag_close = '</b>'
		)
	{
		$use_db = $this->{$prm_dbase};
		$menu_structure = array();

		$use_db
			->select('id, type, parent_id, title, content, slug')
			->from('biskitz_front_menu')
			->where
				(
					array
					(
						'language__id' => $this->web_data['BISKITZ_FRONT']['LANGUAGE'][0]['id']
						, 'parent_id' => $prm_parent_id
						, 'status' => 'Publish'
					)
				)
			->order_by('orders, title');
		$qry = $this->db->get();

		if ($qry->num_rows() > 0)
		{
			foreach ($qry->result() as $rows)
			{
				/**
				 * @description check if there is any sub menu or not
				 */
				$menu_sub_count = $this->M_database->total_rows
					(
						$prm_dbase
						, 'biskitz_front_menu'
						, array
							(
								'language__id' => $this->web_data['BISKITZ_FRONT']['LANGUAGE'][0]['id']
								, 'parent_id' => $rows->id
								, 'status' => 'Publish'
							)
					);

				switch ($rows->type)
				{
					case 'Page':
						$menu_url = site_url(array('read', 'pages', $rows->slug));
						break;

					default:
						$menu_url = strip_tags($rows->content);
						break;
				}

				if ($prm_menu_sub_use == TRUE && $menu_sub_count > 0)
				{
					if ($prm_parent_as_link == TRUE)
					{
						$menu_anchor = anchor
							(
								'#'
								, $rows->title
								, array
									(
										'onclick' => 'return false'
										, 'class' => ($rows->parent_id == 0) ? 'menu_main' : 'menu_sub'
									)
							);

					}
					else
					{
						$menu_anchor = $prm_parent_tag_open . $rows->title . $prm_parent_tag_close;
					}


					$menu_structure[$menu_anchor] = $this->front_generate
							(
								$prm_dbase
								, $prm_language_abbr
								, $prm_menu_sub_use, $rows->id
								, $prm_parent_as_link
							);

				}
				else
				{
					$menu_structure[$rows->title] = anchor
						(
							$menu_url
							, htmlentities($rows->title)
							, array('title' => htmlentities($rows->title))
						);
				}

			}
		}


		return $menu_structure;
	}



}

/** End of file m_menu.php */
/** Location: ./biskitz_mpc/biskitz/models/biskitz/m_menu.php */